package cn.tedu.mall.seckill.consumer;

import cn.tedu.mall.pojo.seckill.model.Success;
import cn.tedu.mall.seckill.config.RabbitMqComponentConfiguration;
import cn.tedu.mall.seckill.mapper.SeckillSkuMapper;
import cn.tedu.mall.seckill.mapper.SeckillSuccessMapper;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author BJ
 * @date 2022/11/11 19:40
 * RabbitMQ消息队列，用来完成减库存和保存Success信息到数据库
 */
@Component
@RabbitListener(queues = RabbitMqComponentConfiguration.SECKILL_QUEUE)
public class RabbitQueueConsumer {
    @Autowired
    private SeckillSkuMapper skuMapper;

    @Autowired
    private SeckillSuccessMapper successMapper;

    @RabbitHandler
    public void process(Success success){
        skuMapper.updateSeckillSkuStockBySkuId(success.getSkuId(),success.getQuantity());
        successMapper.saveSuccess(success);

    }
}
